using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using RaisingStudio.SmallBasicCompiler.Expressions;

namespace RaisingStudio.SmallBasicCompiler.Statements
{
    public class ElseIfStatement : Statement
    {
        public List<Statement> ThenStatements = new List<Statement>();
        public Expression Condition;
        public TokenInfo ElseIfToken;
        public TokenInfo ThenToken;
        public override void AddSymbols(SymbolTable symbolTable)
        {
            if (this.Condition != null)
            {
                this.Condition.AddSymbols(symbolTable);
            }
            foreach (Statement current in this.ThenStatements)
            {
                current.AddSymbols(symbolTable);
            }
        }
        public override void PrepareForEmit(CodeGenScope scope)
        {
            foreach (Statement current in this.ThenStatements)
            {
                current.PrepareForEmit(scope);
            }
        }
        public override int GetIndentationLevel(int line)
        {
            if (line == this.ElseIfToken.Line)
            {
                return 0;
            }
            Statement statementContaining = Statement.GetStatementContaining(this.ThenStatements, line);
            if (statementContaining != null)
            {
                return 1 + statementContaining.GetIndentationLevel(line);
            }
            return 1;
        }
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(CultureInfo.CurrentUICulture, "{0} {1}\r\n", new object[]
			{
				this.ElseIfToken.Text, 
				this.Condition
			});
            foreach (Statement current in this.ThenStatements)
            {
                stringBuilder.AppendFormat(CultureInfo.CurrentUICulture, "  {0}", new object[]
				{
					current
				});
            }
            return stringBuilder.ToString();
        }
    }
}
